import { LogHelper } from "../../utils/logHelper";
import { PosHelper } from "../../utils/posHelper";

/**
 * 外矿旗帜命令
 */
export class OuterOreFlagTask implements IFlagTask {
  run(flag: Flag): void {
    const closestRoomName = PosHelper.getClosestMyRoom(flag.pos.roomName);
    const room = Game.rooms[flag.pos.roomName];

    if(room){
      if(!room.controller){
        LogHelper.error(`Flag[${flag.name}] 发生错误，错误原因：房间没有控制器，不可设为外矿`);
        flag.remove();
        return;
      }

      flag.remove();
      const managerFlagName = `外矿房间管理:${flag.pos.roomName}`;
      room.createFlag(25,25,managerFlagName,COLOR_YELLOW,COLOR_YELLOW);
      const managerFlag = Game.flags[managerFlagName];
      if(closestRoomName){
        managerFlag.memory.outerOre = { closestRoom : closestRoomName,room: room.name}
      }
      delete Memory.flags[flag.name];
    }
    else {
      if(!flag.memory.viewRequest){
        // 申请派出侦察兵
        LogHelper.warn(`Flag[${flag.name}] 房间 ${flag.pos.roomName} 未有视野，申请派出探路者`);
        if(closestRoomName){
          Game.rooms[closestRoomName].spawnQueueAdd('pathfinder',[MOVE,MOVE,MOVE,MOVE,MOVE,MOVE],{role:'pathfinder',bornRoom:closestRoomName,targetRoom:flag.pos.roomName});
          flag.memory.viewRequest = true;
        }
      }
    }
  }
}